<?php
/**
 * pc_menu.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-07 22:09
 * @modified 2018-07-07 22:09
 */

class ControllerSettingPcMenu extends Controller
{
    private $error = array();

    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->load->language('setting/pc_menu');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('setting/pc_menu');
        $this->load->model('localisation/language');
    }

    public function index()
    {
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

            $this->model_setting_pc_menu->editMenu($this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('setting/pc_menu', 'user_token=' . $this->session->data['user_token'], true));
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        $data['languages'] = $this->model_localisation_language->getLanguages();

        $breads = new Breads();
        $breads->add(trans('text_home'), uriLink('common/dashboard', 'user_token=' . $this->session->data['user_token']));
        $breads->add(trans('heading_title'), uriLink('setting/pc_menu', 'user_token=' . $this->session->data['user_token']));
        $data['breadcrumbs'] = $breads->get();

        if (isset($this->request->post['menu'])) {
            $data['menu'] = $this->request->post['menu'];
        } else {
            $data['menu'] = $this->model_setting_pc_menu->getMenu();
        }

        $data['cancel'] = $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true);

        $this->response->setOutput($this->load->view('setting/pc_menu', $data));
    }

    protected function validateForm()
    {
        if (!$this->user->hasPermission('modify', 'catalog/product')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        return !$this->error;
    }
}